package org.sunshine.miracle.ui;

import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Decorations;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.Widget;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.custom.TreeEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.ModifyEvent;

public class SWTUtils {

    public Display display ;

    private static SWTUtils instance;

    static SWTUtils getInstance(){
        if (instance == null){
            instance = new SWTUtils();
        }
        return instance;
    }


    public SWTUtils() {
        display = new Display();
    }


    public static void loop(Widget widget){
        while (!widget.isDisposed()) {
            if (!getInstance().display.readAndDispatch()) getInstance().display.sleep();
        }
        getInstance().display.dispose();
        System.exit(0);
    }

    public static Shell createShell(int style){

        Shell shell = new Shell(getInstance().display);
        return shell;
    }

    public static Button createButton(Composite parent, int style){
        Button button = new Button(parent, style);
        return button;
    }

    public static Menu createMenu(Decorations parent, int style){
        Menu menu = new Menu(parent, style);
        return menu;
    }

    public static Menu createMenu(Menu parent){
        Menu menu = new Menu(parent);
        return menu;
    }

    public static Menu createMenu(Control parent){
        Menu menu = new Menu(parent);
        return menu;
    }

    public static MenuItem createMenuItem(Menu parent, int style){
        MenuItem menuItem = new MenuItem(parent, style);
        return menuItem;
    }


    public static void setCenter(Shell shell){
        int displayHeight = getInstance().display.getBounds().height;
        int displayWidth = getInstance().display.getBounds().width;
        int x = (displayWidth - shell.getSize().x) / 2;
        int y = (displayHeight - shell.getSize().y) / 2;
        shell.setLocation(x, y);

    }

    public static DirectoryDialog createDirecotoryDialog(Shell parent, int style, String text, String message){
        DirectoryDialog dialog = new DirectoryDialog (parent, style);
        dialog.setText(text);
        dialog.setMessage(message);
        return dialog;
    }


    public static void showErrorMessageBox(Shell parent, String message){
        MessageBox dialog = new MessageBox(parent, SWT.ICON_ERROR);
        dialog.setText("Error");
        dialog.setMessage(message);
        dialog.open();


    }

    public static Label createLabel(Composite composite, int style) {
        Label label = new Label(composite, style);
        return label ; 
    }
}
